﻿using _11._21._2014_AngularWebAPI.Data;
using _11._21._2014_AngularWebAPI.Data.Models;
using _11._21._2014_AngularWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _11._21._2014_AngularWebAPI.adapters.Data
{
    public class MessageDataAdapter : IMessageAdapter
    {
        public List<MessageViewModel> GetMessageViewModels()
        {
            List<MessageViewModel> models = null;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                models = db.Messages.Select(m => new MessageViewModel
                {
                    Id = m.Id,
                    Name = m.Name,
                    Text = m.Text,
                    Time = m.Time
                }).ToList();
            }

            return models;
        }

        public int CreateMessage(MessageViewModel model)
        {
            int id = 0;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Message dbMessage = new Message
                {
                    Name = model.Name,
                    Text = model.Text,
                    Time = model.Time
                };

                db.Messages.Add(dbMessage);
                db.SaveChanges();

                id = dbMessage.Id;
            }

            return id;
        }
    }
}